package org.jeecg.modules.xxgx.sjtb.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.config.TenantContext;
import org.jeecg.modules.jfptpz.entity.CsPtJfptpz;
import org.jeecg.modules.jfptpz.service.ICsPtJfptpzService;
import org.jeecg.modules.system.service.ISysDictService;
import org.jeecg.modules.xxgx.fpgl.entity.CsXxFpxx;
import org.jeecg.modules.xxgx.fpgl.service.ICsXxFpxxService;
import org.jeecg.modules.xxgx.sjtb.param.FpxxSyncParam;
import org.jeecg.modules.xxgx.sjtb.service.ISjtbService;
import org.jeecg.modules.xxgx.sjtb.util.SjtbRequestUtil;
import org.jeecg.modules.xxgx.sjtb.vo.FpxxSyncRtn;
import org.jeecg.modules.xxgx.sjtb.vo.SjtbApiResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SjtbServiceImpl implements ISjtbService {
    @Autowired
    private ICsXxFpxxService csXxFpxxService;
    @Autowired
    private ISysDictService sysDictService;

    @Autowired
    private ICsPtJfptpzService csPtJfptpzService;

    @Override
    public Result<String> pushFpxx(String fpxxid) {
        //取配置信息
        CsPtJfptpz csPtJfptpz = csPtJfptpzService.getCurJfptpz(TenantContext.getTenant());
        if(csPtJfptpz == null || !"1".equals(csPtJfptpz.getKqsjsc())){
            return Result.error("未开启数据同步");
        }

        CsXxFpxx csXxFpxx = csXxFpxxService.getById(fpxxid);
        if(csXxFpxx == null) {
            return Result.error("发票信息不存在");
        }
        FpxxSyncParam param = new FpxxSyncParam();
        BeanUtils.copyProperties(csXxFpxx,param);
        SjtbRequestUtil sjtbRequestUtil = new SjtbRequestUtil();
        sjtbRequestUtil.setServerUrl(csPtJfptpz.getServerUrl());
        sjtbRequestUtil.setAppKey(csPtJfptpz.getAppkey());
        sjtbRequestUtil.setAppSecret(csPtJfptpz.getAppsecret());
        SjtbApiResponse response = sjtbRequestUtil.request("/api/sjtb/fpxxSync", JSONObject.toJSONString(param));
        if(response.isSuccess()) {
            //返回发票交付地址
            FpxxSyncRtn fpxxSyncRtn = JSONObject.toJavaObject(response.getResult(), FpxxSyncRtn.class);
            if(StringUtils.isNotBlank(fpxxSyncRtn.getFpjfurl())){
                CsXxFpxx csXxFpxxUpdate  = new CsXxFpxx();
                csXxFpxxUpdate.setId(fpxxSyncRtn.getId());
                csXxFpxxUpdate.setTenantId(csXxFpxx.getTenantId());
                csXxFpxxUpdate.setFpjfurl(fpxxSyncRtn.getFpjfurl());
                csXxFpxxService.updateById(csXxFpxxUpdate);
            }
            return Result.OK("成功",fpxxSyncRtn.getFpjfurl());
        } else {
            log.error("同步发票信息出错：" + response.getMessage());
            return Result.error("同步发票信息出错：" + response.getMessage());
        }
    }

    @Override
    public void tbfpxx(String fphm) {
        QueryWrapper<CsXxFpxx> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fphm",fphm);
        List<CsXxFpxx> list = csXxFpxxService.list(queryWrapper);
        if(list.isEmpty()){
            return;
        }
        CsXxFpxx csXxFpxx = list.get(0);
        this.pushFpxx(csXxFpxx.getId());
    }
}
